<template>
    <a-drawer :title="`生成出货单`" width="70%" :visible="visible"
        hide-cancel
        @cancel="visible = false"
        :footer="false"
        >
        <a-table :columns="columns" :data="dataList" >
            <template #columns>
                <a-table-column title="货站" data-index="depot_id">
                    <template #cell="{ record }">
                        {{ record.depot.name }}
                    </template>
                </a-table-column>
                <a-table-column title="运单数" data-index="shipment_count"></a-table-column>
                <a-table-column title="货箱数" data-index="parcel_count"></a-table-column>
                <a-table-column title="操作">
                    <template #cell="{ record }">
                        <a-button @click="ceateDelivery(record)">生成转运单</a-button>
                    </template>
                </a-table-column>

            </template>
        </a-table>
        <add-modal ref="addModalRef"></add-modal>
    </a-drawer>
</template>

<script setup>
import {computed, inject, ref,provide,reactive,watch} from "vue";
import waybill from "@/api/document/waybill.js";
import AddModal from "@cps/actions/addModal.vue";
import { getDictData } from '@/utils/crud/dict'
import dictHelp from "@/utils/dicthelp.js";
import { auth, role } from '@/utils/common'

const visible = defineModel()
const record = inject('record')
const columns = []
let dataList = []
const addModalRef = ref()
const getData = async () => {
    if(auth('document:waybill:delivery')){
        let res = await waybill.delivery({id:record.value.id})
        dataList = res.data
    }
}

getData()

watch(visible,async (value)=>{
    console.log(value)
    if(value == true){
        getData()
    }
})

const ceateDelivery = (data) =>{
    console.log(data)
    addModalRef.value.open(
     reactive([
        {
            title: "发出站点",
            dataIndex: "from_depot_id",
            formType: "select",
            disabled: true,
            addDefaultValue:data.depot.id,
            dict: {
                url: 'warehouse/depot/list',
                props: {label: 'name', value: 'id'},
                translation: true,
                params: {
                    select: 'id,name',
                }
            },
            commonRules: {
                required: true,
                message: "请选择站点"
            }
        },
        {
            title: "发往站点",
            dataIndex: "to_depot_id",
            formType: "select",
            dict: {
                url: 'warehouse/depot/list',
                props: {label: 'name', value: 'id'},
                translation: true,
                params: {
                    select: 'id,name',
                }
            },
            commonRules: {
                required: true,
                message: "请选择站点"
            }
        },
        {
            title: "类型",
            dataIndex: "type",
            formType: "select",
            addDefaultValue:1,
            search: true,
            dict:{
                data:[
                    {label: "大货",value: "1"},
                    {label: "包裹",value: "2"}
                ],
                translation: true
            },
            commonRules: {
                required: true,
                message: "请输入类型"
            }
        },
        {
            title: "供应商服务",
            dataIndex: "supplier_service_id",
            virtualListProps:{},
            formType: "select",
            dict: {
                url: 'logistics/supplier/list',
                props: {
                    label: 'name',
                    value: 'id'
                },
                translation: true
                },
        },
        {
            title: "参考号",
            dataIndex: "reference",
            formType: "input",
        },
        {
            title: "主品名",
            dataIndex: "name",
            formType: "input"
        },
        {
            title: "单独清关",
            dataIndex: "clearance",
            formType: "switch",
            checkedValue: 1,
            uncheckedValue: 0,
        },
        {
            title: "出口报关",
            dataIndex: "declaration",
            formType: "switch",
            checkedValue: 1,
            uncheckedValue: 0,
        },
        {
            title: "标识",
            dataIndex: "tags",
            dict:dictHelp.tags(3),
            formType: "checkbox",
            customRender: ({record}) => {
            return getDictData('tags',record.tags,crudRef)
            }
        },
        {
            title: "分货备注",
            dataIndex: "distribut_mark",
            formType: "input",
        },
        {
            title: "备注",
            dataIndex: "remark",
            formType: "textarea",
            hide: true,
        },
        {
            title: "运单",
            dataIndex: "shipment_nos",
            formType: "input",
            disabled: true,
            addDefaultValue:data.shipment_no
        },
    ]) , {
    viewType: 'modal',
    width: '50%',
    layout: [],
  }, waybill.createDelivery,'生成转运单',function name(params) {
      
  })
}

</script>

